#ifndef CX_PWM_H
#define CX_PWM_H
#include "cx_typedef.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx.h"
#define CX_PWM_HANDLER_SET_FREQ (CX_HANDLER_CLASS_SET | 0x00)
#define CX_PWM_HANDLER_SET_PULSE (CX_HANDLER_CLASS_SET | 0x01)

#define CX_PWM_HANDLER_GET_FREQ (CX_HANDLER_CLASS_GET | 0x00)
#define CX_PWM_HANDLER_GET_PULSE (CX_HANDLER_CLASS_GET | 0x01)
#define CX_PWM_HANDLER_GET_STATUS (CX_HANDLER_CLASS_GET | 0x02)

#define CX_PWM_HANDLER_INTR_PULSE_FINISH 0x00

#define CX_PWM_HANDLER_SUB_CLASS_INTR_ENABLE  0x00000100
#define CX_PWM_HANDLER_SUB_CLASS_INTR_DISABLE 0x00000000
#define CX_PWM_HANDLER_CFG_DISABLE_INTR (CX_HANDLER_CLASS_CFG | CX_PWM_HANDLER_SUB_CLASS_INTR_DISABLE)
#define CX_PWM_HANDLER_CFG_ENABLE_INTR (CX_HANDLER_CLASS_CFG | CX_PWM_HANDLER_SUB_CLASS_INTR_ENABLE)

#define CX_PWM_HANDLER_CTRL_STOP   (CX_HANDLER_CLASS_CTRL | 0x00)
#define CX_PWM_HANDLER_CTRL_START  (CX_HANDLER_CLASS_CTRL | 0x01)

typedef struct pwm_handler
{
    void (*cbHandler)(struct pwm_handler *self,cx_uint32_t HandleAttr, void * value);
    void (*cbIRQHandler)(struct pwm_handler *self,cx_uint32_t CX_PWM_HANDLER_INTR,void *args);
    cx_uint32_t pulse;
    cx_uint32_t freq;
    cx_uint32_t flags;

    TIM_TypeDef *htim;
    cx_uint16_t channel;
    cx_uint32_t FreqK;
}pwm_handler_t;

void STM32F4xx_PWM_IRQHandler(struct pwm_handler *self,cx_uint32_t CX_PWM_HANDLER_INTR,void *args);
int CX_PWM_ON_OFF(pwm_handler_t *pwm, cx_uint32_t CX_PWM_ON_OFF);
int CX_PWM_SetPulse(pwm_handler_t *pwm, cx_uint32_t pulse);
int CX_PWM_SetFrequency(pwm_handler_t *pwm, cx_uint32_t frequency_Hz);
int CX_PWM_CfgIntr(pwm_handler_t *pwm, cx_uint32_t CX_PWM_HANDLER_CFG_INTR);
void CX_PWM_IRQHandler(pwm_handler_t *pwm,cx_uint32_t CX_PWM_HANDLER_INTR,void *args);

extern pwm_handler_t TIM2_Channel_1_GPIOA5;

#endif
